package curves;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;


public class PanelAffichage extends JPanel{
	
	private static final long serialVersionUID = 1L;

	private double pas = 0.1;
	
	public PanelAffichage(final CurveControlsUser cv, final FunctionVariations var,   final CurveFrame f){
		super();
		
		final CurveTracerUser ctu = f.getTracer();
		
		
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		this.setBorder(new EtchedBorder());
	
//		 ----- PRECISION -----
		JPanel precision = new JPanel();
		JLabel title = new JLabel("Precision");
		cv.cb = new JComboBox(cv.getNStepsChoices());
		cv.cb.addItemListener((ItemListener) (new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					f.getTracer().tabulateThemAll(cv.currentPrecision());
					f.infos.update();
					f.repaint();
				}
			}
		}));
		cv.cb.setSelectedIndex(cv.getNStepsChoices().length / 2);
		precision.add(title);
		precision.add(cv.cb);
		this.add(precision);
//		 ----- END PRECISION -----
		
//		 ----- EPAISSEUR COURBE -----
		Integer thickness_choice[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
		JPanel thickness = new JPanel();
		JLabel thickness_label = new JLabel("Epaisseur");
		final JComboBox cb_thickness = new JComboBox(thickness_choice);
		cb_thickness.addItemListener((ItemListener) (new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					f.getTracer().setThickness(cb_thickness.getSelectedIndex());
					f.repaint();
				}
			}
		}));
		
		thickness.add(thickness_label);
		thickness.add(cb_thickness);
		this.add(thickness);
//		 ----- END EPAISSEUR COURBE -----
		
		
//		 ----- ZOOM -----
		JPanel zoom = new JPanel();
	
		TitledBorder zoomTitle;
		zoomTitle = BorderFactory.createTitledBorder("Zoom");
		zoom.setBorder(zoomTitle);

		JButton plus = new JButton("+");
		JButton moins = new JButton("-");
		JButton defaut = new JButton("Zoom par defaut");
	
		double rapport_plus = 3.0/4.0;
		double rapport_moins = 4.0/3.0;
	
		
		
		final double mindefaut = ctu.getXmin();
		final double maxdefaut = ctu.getXmax();
	
		class ZoomListener implements ActionListener{
			public double rapport = 3.0/4.0;
			public boolean defaut;
			public void actionPerformed(ActionEvent e)
				{
				if(defaut){
					ctu.setXmin(mindefaut);
					ctu.setXmax(maxdefaut);

				}
				else {
					ctu.setXmin(ctu.getXmin() * rapport);
					ctu.setXmax(ctu.getXmax() * rapport);
				}
				ctu.tabulateThemAll(cv.currentPrecision());
				f.infos.update();
				f.repaint();
			}
		public void setDefaut(){
			this.defaut = true;
		}
		public void setRapport(double rapport){
			this.rapport = rapport;
			this.defaut = false;
			}
		}
		
		ZoomListener listener_plus = new ZoomListener();
		ZoomListener listener_moins = new ZoomListener();
		ZoomListener listener_defaut = new ZoomListener();

		listener_plus.setRapport(rapport_plus);	
		plus.addActionListener(listener_plus);
		zoom.add(plus);		

		listener_defaut.setDefaut();
		defaut.addActionListener(listener_defaut);
		zoom.add(defaut);
	
		listener_moins.setRapport(rapport_moins);
		moins.addActionListener(listener_moins);
		zoom.add(moins);
	
		this.add(zoom);
		
		
		//ROULETTE & CLICK
		
		ctu.addMouseListener(new MouseAdapter() {

			double min;
			double max;
			public void mouseClicked(MouseEvent e) {
				if(! ctu.getZoom()){
					double taille = ctu.getXmax() - ctu.getXmin();
					max = ctu.getXmax();
					min = ctu.getXmin();
					ctu.setXmin(ctu.realX(e.getX()) - (taille/4) );
					ctu.setXmax(ctu.realX(e.getX()) + (taille/4) );
					ctu.setZoom(true);
				}
				else{
					ctu.setXmin(min);
					ctu.setXmax(max);
					ctu.setZoom(false);
				}
				ctu.tabulateThemAll(f.controls.currentPrecision());
				f.infos.update();
				ctu.repaint();
			}
			
		});
		
		
		ctu.addMouseWheelListener(new MouseWheelListener(){
			double rapport;
			public void mouseWheelMoved(MouseWheelEvent e) {
				if(e.getWheelRotation() < 0)
					rapport = 0.85;
				else rapport = 1.0/0.85;
				
				ctu.setXmin(ctu.getXmin() * rapport);
				ctu.setXmax(ctu.getXmax() * rapport);
				ctu.tabulateThemAll(f.controls.currentPrecision());
				f.infos.update();
				f.repaint();
			}
		});
//		 ----- END ZOOM -----
		
//		 ----- DEPLACEMENT -----
		JPanel deplacement = new JPanel();
		deplacement.setLayout(new FlowLayout());
	
		JToolBar toolBar = new JToolBar("Deplacement");
	 
		TitledBorder deplacementTitle;
		deplacementTitle = BorderFactory.createTitledBorder("Deplacements");
		deplacement.setBorder(deplacementTitle);
		
		class DeplacementListener implements ActionListener{
			int dir;
			public void actionPerformed(ActionEvent e)
				{
				if(dir < 0){
				
				ctu.setXmin(ctu.getXmin() + pas);
				ctu.setXmax(ctu.getXmax() + pas);
				}
				else {
				ctu.setXmin(ctu.getXmin() - pas);
				ctu.setXmax(ctu.getXmax() - pas);
				}
				ctu.tabulateThemAll(cv.currentPrecision());
				f.infos.update();
				f.repaint();
				}
			public void setDirection(int i){
				dir = i;
			}
		}
	 
		DeplacementListener d_droit = new DeplacementListener();
		DeplacementListener d_gauche = new DeplacementListener();
		d_droit.setDirection(-1); //Sens de la direction : négatif => on avance
		d_gauche.setDirection(1); //sinon on recule
	 
	 		 
		JButton droite = new JButton();
		droite.setIcon(new ImageIcon("./forward.gif", "Deplacement droite"));
		droite.addActionListener(d_droit);
	 
		JButton gauche = new JButton();
		gauche.setIcon(new ImageIcon("./back.gif", "Deplacement gauche"));
		gauche.addActionListener(d_gauche);
	 
		final JTextField pasField = new JTextField(4);
		pasField.setText("1");
		CaretListener caretupdate = new CaretListener(){
			public void caretUpdate(CaretEvent e) {
				JTextField text = (JTextField)e.getSource();
				pas = Integer.parseInt("0"+text.getText());
			}
		};
		pasField.addCaretListener(caretupdate);
				
		deplacement.add(new JLabel("Delta :\n"));
		deplacement.add(pasField);
		toolBar.add(gauche);
		toolBar.add(droite);		 
		deplacement.add(toolBar);
	
		this.add(deplacement);
//		 ----- END DEPLACEMENT -----
		
		}
}
